// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	namespace Utils
	{
		public sealed class SMOExt
		{
			
			
			static ArrayList GetServerList_serverList = default(ArrayList);
		
			public static ArrayList GetServerList(bool refresh = false)
			{
				// static ArrayList serverList = default(ArrayList); VBConversions Note: Static variable moved to class level and renamed GetServerList_serverList. Local static variables are not supported in C#.
				if (GetServerList_serverList == null || refresh)
				{
					DataTable dtb = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers();
					try
					{
						GetServerList_serverList = (ArrayList) (new ArrayList());
						if (dtb.Rows.Count > 0)
						{
							foreach (DataRow dr in dtb.Rows)
							{
								GetServerList_serverList.Add(dr["Name"]);
							}
						}
						return GetServerList_serverList;
					}
					catch
					{
						return null;
					}
					finally
					{
						if (dtb != null)
						{
							dtb.Dispose();
						}
					}
				}
				else
				{
					return GetServerList_serverList;
				}
			}
			
			
			static ArrayList GetNetworkServerList_serverList = default(ArrayList);
			/// <summary>
			/// 
			/// </summary>
			/// <param name="computerName"></param>
			/// <param name="refresh"></param>
			/// <returns></returns>
			public static ArrayList GetNetworkServerList(string computerName, bool refresh = false)
			{
				// static ArrayList serverList = default(ArrayList); VBConversions Note: Static variable moved to class level and renamed GetNetworkServerList_serverList. Local static variables are not supported in C#.
				if (GetNetworkServerList_serverList == null || refresh)
				{
					DataTable dtb = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers(computerName);
					try
					{
						GetNetworkServerList_serverList = (ArrayList) (new ArrayList());
						if (dtb.Rows.Count > 0)
						{
							foreach (DataRow dr in dtb.Rows)
							{
								GetNetworkServerList_serverList.Add(dr["Name"]);
							}
						}
						return GetNetworkServerList_serverList;
					}
					catch
					{
						return null;
					}
					finally
					{
						if (dtb != null)
						{
							dtb.Dispose();
						}
					}
				}
				else
				{
					return GetNetworkServerList_serverList;
				}
			}
			
			// VBConversions Note: Former VB local static variables moved to class level.
			static ArrayList GetLocalServerList_serverList = default(ArrayList);
			

            /// <summary>
            /// Return a list of ONLY Local Server
            /// </summary>
            /// <param name="refresh"></param>
            /// <returns></returns>
			public static ArrayList GetLocalServerList(bool refresh = false)
			{
				// static ArrayList serverList = default(ArrayList); VBConversions Note: Static variable moved to class level and renamed GetLocalServerList_serverList. Local static variables are not supported in C#.
				if (GetLocalServerList_serverList == null || refresh)
				{
					DataTable dtb = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers(true);
					try
					{
						GetLocalServerList_serverList = (ArrayList) (new ArrayList());
						if (dtb.Rows.Count > 0)
						{
							foreach (DataRow dr in dtb.Rows)
							{
								GetLocalServerList_serverList.Add(dr["Name"]);
							}
						}
						return GetLocalServerList_serverList;
					}
					catch
					{
						return null;
					}
					finally
					{
						if (dtb != null)
						{
							dtb.Dispose();
						}
					}
				}
				else
				{
					return GetLocalServerList_serverList;
				}
			}
			
			
			/// <summary>
			/// Uses SQLDMO to get a list of database availble on the specified server.
			/// </summary>
			/// <param name="serverName">Name of server to connect to.</param>
			/// <param name="isTrusted">true to use a trusted connection; false otherwise</param>
			/// <param name="userName">Username to use if not using a trusted connection.</param>
			/// <param name="password">Password to use if not using a trusted connection.</param>
			/// <returns>An ArrayList containing the names of the databases found.</returns>
			/// <remarks></remarks>
			public static ArrayList GetDatabaseList(string serverName, bool isTrusted, string userName, string password)
			{
				ArrayList alDBList = (ArrayList) (new ArrayList());
				Microsoft.SqlServer.Management.Common.ServerConnection svrCon = new Microsoft.SqlServer.Management.Common.ServerConnection(serverName, userName, password);
				svrCon.LoginSecure = isTrusted;
				Microsoft.SqlServer.Management.Smo.Server svr = new Microsoft.SqlServer.Management.Smo.Server(svrCon);
				Database db = default(Database);
				
				try
				{
					foreach (Database tempLoopVar_db in svr.Databases)
					{
						db = tempLoopVar_db;
						if (db.Name != null)
						{
							alDBList.Add(db.Name);
						}
					}
				}
				catch
				{
					alDBList = (ArrayList) null;
				}
				return alDBList;
			}
			
			/// <summary>
			/// Parse an SqLStatement Like in Sql Management Studio
			/// </summary>
			/// <param name="sqlStatement"></param>
			/// <remarks></remarks>
			public static void ParseSQLStatement(string sqlStatement)
			{
				
				Microsoft.SqlServer.Management.Common.ServerConnection svrCon = new Microsoft.SqlServer.Management.Common.ServerConnection(Base.AppManager.DbServerName, Base.AppManager.UserName, Base.AppManager.Password);
				svrCon.LoginSecure = Base.AppManager.IsTrustedConnection;
				Microsoft.SqlServer.Management.Smo.Server svr = new Microsoft.SqlServer.Management.Smo.Server(svrCon);
				Database db = default(Database);
				db = svr.Databases[Base.AppManager.DbName];
				//Run a Transact-SQL command on the database.
				db.ExecuteNonQuery(sqlStatement, Microsoft.SqlServer.Management.Common.ExecutionTypes.NoCommands);
				
			}
		}
	}
	
}
